home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / ArchiveUtils / JumpBack / Source / JumpBackControl.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  8.9 KB  |  366 lines

  1.  
  2. //======================================================================
  3. //
  4. //    Portions written by FreemanSoft Inc.
  5. //
  6. //    FreemanSoft disclaims any warranty of any kind, expressed or implied,
  7. //    as to this source code's fitness for any particular use.
  8. //
  9. //    For more information, use the following electronic mail addresses:
  10. //     
  11. //        info@FreemanSoft.com    general questions
  12. //        support@FreemanSoft.com    technical questions
  13. //
  14. //======================================================================
  15.  
  16.  
  17. /* Written by
  18.  *    Joe Freeman    jfreeman@next.com    
  19.  *    JumpBackControl
  20.  *
  21.  *    This code has no warranty.  
  22.  *    It is provided so that the consumer may maintain and modify it
  23.  *    at their own risk.  Use this code at your own risk.
  24.  */
  25.  
  26. #import "JumpBackControl.h"
  27. #import "RdistControl.h"
  28. #import "CDROMIndexControl.h"
  29. #import "TarControl.h"
  30. #import "PreferencesPanel.h"
  31. #import "common.h"
  32. #import "pathutil.h"
  33. #import <sys/vnode.h>        /* AAACHH!  unix dweebie stuff !! */
  34.  
  35. @implementation JumpBackControl
  36.  
  37. /*================================================================
  38.  *
  39.  *================================================================*/
  40.  
  41.     
  42. /*================================================================
  43.  *
  44.  *================================================================*/
  45.  
  46. - init
  47. {
  48.     [super init];
  49.     
  50.     dragBackList = [[List alloc] init];
  51.     scriptBackList = [[List alloc] init];
  52.     return self;
  53. }
  54.  
  55. - free
  56. {
  57.     [dragBackList free];
  58.     [scriptBackList free];
  59.     return self;
  60. }
  61.  
  62. - appDidInit:sender
  63. {
  64.     /* YOW!  even the default keys and values are localized */
  65.     const NXDefaultsVector JumpBackDefaults = {
  66.         {info_default,     yes_str},
  67.     {show_drag,    yes_str},
  68.     {path_default,     rdist_relative},
  69.     {dup_default,     dup_update},
  70.     {find_no_write,    yes_str},
  71.     {debug,        "0"},
  72.     {NULL}
  73.     };
  74.     char buf[MAXPATHLEN+1];    
  75.  
  76.     NXRegisterDefaults([NXApp appName], JumpBackDefaults);
  77.     
  78.     /* make our custom lib dir first -- so other modules don't have to worry */
  79.     /* ignore return  because we don't care if the dir is already there */
  80.     strcpy(buf, NXHomeDirectory());
  81.     strcat(buf,"/");
  82.     strcat(buf,lib_dir);
  83.     mkdir(buf, VREAD | VWRITE | VEXEC );
  84.  
  85.     /* build our predefined rdist's menu */
  86.     [self loadPredefined:self];
  87.     
  88.     prefPanel = [PreferencesPanel new];
  89.     
  90.     /* load the info panel if novice user */
  91.     if (!strcmp(NXGetDefaultValue([NXApp appName],info_default), yes_str))
  92.     [self showInfoPanel:self];
  93.  
  94.     /* show a drag style (standard) JumpBack window if user wants it */
  95.     if (!strcmp(NXGetDefaultValue([NXApp appName], show_drag), yes_str))
  96.     [self newDragController:self];
  97.  
  98.     return self;
  99. }
  100.  
  101. - appWillTerminate:sender
  102. {
  103.     int i;
  104.     int lastDragBack ;
  105.     
  106.     for ( i = 0 ; i < [dragBackList count]; i++)
  107.     {
  108.         if ([[dragBackList objectAt:i] state] != NOTHING_RUNNING)
  109.     {
  110.                if (    NXRunAlertPanel([NXApp appName],
  111.             something_running,
  112.             dont_close, close_anyway,NULL) == NX_ALERTDEFAULT )
  113.             return nil;
  114.         else
  115.             break;
  116.     }
  117.     }
  118.     
  119.     lastDragBack= [dragBackList count]-1;
  120.     
  121.     for ( i = lastDragBack  ; i >= 0  ; i--)
  122.     {
  123.         /* no way to detect window wasn't closed */
  124.         [[dragBackList objectAt:i] abortProcess:self];
  125.         [[[dragBackList objectAt:i] window] performClose:self];
  126.     }
  127.     return self;
  128. }
  129.  
  130. /*================================================================
  131.  *
  132.  *================================================================*/
  133.  
  134. - loadPredefined:sender
  135. {
  136.     NXStream *    stream;
  137.     char *    lineStart;        /* start of current line in data */
  138.     char *    tab1;            /* tab that is end of menu */
  139.     char *    tab2;            /* tab that is end of src */
  140.     
  141.     char *    data;            /* stream buffer */
  142.     int     length;
  143.     int     maxLength;
  144.     
  145.     int     i;
  146.     MenuCell *    newCell;        /* while we are building the menu */
  147.     
  148.     stream = [self mapResource:pre_def_file];
  149.     
  150.     /* get the buffer , and count the number of lines */
  151.     NXGetMemoryBuffer(stream, &data, &length, &maxLength);
  152.     if (length == 0){
  153.         NXClose(stream);
  154.     return nil;
  155.     }
  156.     
  157.     /* figure out how many lines there are */
  158.     pairNum = 0;
  159.     lineStart = data;
  160.     tab1 = tab2 = NULL;
  161.     /* see if the first line is a comment line */
  162.     for ( i = 0 ; i <= length; i++){
  163.         if (pairNum >= JBC_MAX_PAIR)
  164.         break;
  165.     
  166.     if ((i < length) && data[i] == '\t')
  167.     {
  168.         if (!tab1 )
  169.             tab1 = &data[i];
  170.         else if (!tab2 )
  171.             tab2 = &data[i];
  172.         else
  173.         {
  174.             if (debug_level >= debug_file_parse)
  175.                 fprintf(stderr,"Found extra tab char\n");
  176.         }
  177.     } else if (data[i] == '\n' || 
  178.             (    (i == length) && 
  179.                 (data[i-1] != '\n') && 
  180.                 (data[i-1] != '\0') ) )
  181.     {
  182.             /* if new line or on last line which had no \n, then at EOL*/
  183.         if (debug_level >= debug_file_parse)
  184.             fprintf(stderr,"Found end of line, char=#%d char=%c\n", 
  185.                         i,*lineStart);
  186.                         
  187.         /* this is a limited check for bogus lines */
  188.         if ( (*lineStart != '#') && tab1 && tab2 && *tab1 && *tab2 ) 
  189.         {
  190.             pairMenu[pairNum] = lineStart;
  191.             
  192.             /* setup src side of pair and replace tab char */
  193.             if (tab1 < &data[i])
  194.             {
  195.                 *tab1 = '\0';
  196.                 pairSrc[pairNum] = &tab1[1];
  197.             }
  198.  
  199.             /* setup dst side of pair and replace tab char */
  200.             if (tab2 < &data[i])
  201.             {
  202.                 *tab2 = '\0';
  203.                 pairDest[pairNum] = &tab2[1];
  204.             }
  205.             
  206.                 /* wipe out the end of line */
  207.             if (data[i] == '\n')
  208.                 data[i] = '\0';
  209.             
  210.             if (debug_level >= debug_file_parse)
  211.                 fprintf(stderr,
  212.                         "Found line "
  213.                         "\tMenu= %s"
  214.                         "\tSrc= %s"
  215.                         "\tDest= %s\n\n", 
  216.                         pairMenu[pairNum],
  217.                         pairSrc[pairNum],
  218.                         pairDest[pairNum]
  219.                     );
  220.             pairNum++;
  221.         } else {
  222.             /* some problem, probably not enough columns */
  223.             if (debug_level >= debug_file_parse)
  224.                 fprintf(stderr,"Not enough columns.\n");
  225.         }
  226.         /* yes, could go past end, but we drop out before problem */
  227.         lineStart = &data[i+1];
  228.         tab1 = tab2 = NULL;
  229.     } else {
  230.         /* some normal old character */
  231.     }
  232.     }
  233.     if (debug_level >= debug_file_parse)
  234.         fprintf(stderr,"Found %d lines.\n", pairNum);
  235.     
  236.     
  237.     /* map the buffer to the menu */
  238.     for ( i = 0 ; i < pairNum; i++){
  239.         newCell = [[pairingsMenu target] 
  240.             addItem:pairMenu[i]
  241.             action:@selector(selectPredefined:)
  242.             keyEquivalent:0];
  243.     [newCell  setTarget:self];
  244.     [newCell  setTag:i];
  245.     }
  246.  
  247.     /* could have been torn off when app quit  ... */
  248.     if ([[pairingsMenu target] isVisible])
  249.         [[pairingsMenu target] display];
  250.     
  251.     /* and close it - keeping the buffer */
  252. ///    NXClose(stream);
  253.     NXCloseMemory(stream, NX_SAVEBUFFER);
  254.     
  255.     return self;
  256. }
  257.  
  258. - selectPredefined:sender
  259. {
  260.     [dragBackList addObject: 
  261.         [[RdistControl alloc] initSourcePath:pairSrc[[sender selectedTag]]
  262.               destinationPath:pairDest[[sender selectedTag]] ] ];
  263.  
  264.     return self;
  265. }
  266.  
  267. /*================================================================
  268.  *
  269.  *================================================================*/
  270.  
  271. - newDragController:sender
  272. {
  273.     [dragBackList addObject: [[RdistControl alloc] init]];
  274.     return self;
  275. }
  276.  
  277. /* catch all */
  278. - newUnrelatedTool:sender
  279. {
  280.     if ([sender selectedTag] == 0)
  281.     {
  282.     [dragBackList addObject: [[CDROMIndexControl alloc] init]];
  283.     }
  284.     else if ([sender selectedTag] == 1)
  285.     {
  286.     [dragBackList addObject: [[TarControl alloc] init]];
  287.     }
  288.     
  289.     return self;
  290. }
  291.  
  292. /*================================================================
  293.  *
  294.  *================================================================*/
  295.  
  296. - preferencesChanged:sender
  297. {
  298.     int i;
  299.     for ( i = 0 ; i < [dragBackList count]; i++){
  300.         [[dragBackList objectAt:i] updateTextFields:self];
  301.     }
  302.     for ( i = 0 ; i < [scriptBackList count]; i++){
  303.         [[scriptBackList objectAt:i] updateTextFields:self];
  304.     }
  305.     return self;
  306. }
  307.  
  308. - dropDragJumpBackController:aController
  309. {
  310.     [dragBackList removeObject:aController];
  311.     return self;
  312. }
  313.  
  314. - (NXStream *)mapResource:(const char *)resourceName
  315. {
  316.     NXStream *    aStream;
  317.     id        bundle;
  318.     char     buf[MAXPATHLEN];
  319.     /* see if the user has a Patterns file */
  320.     strcpy(buf, NXHomeDirectory());
  321.     strcat(buf,"/");
  322.     strcat(buf,lib_dir);
  323.     strcat(buf,"/");
  324.     strcat(buf, resourceName);
  325.     aStream = NXMapFile(buf, NX_READONLY);
  326.     /* if not then use the default one we have in the app wrapper */
  327.     if (!aStream){
  328.         bundle = [NXBundle bundleForClass:[self class]];
  329.         [bundle getPath:buf forResource:resourceName ofType:template];
  330.         aStream = NXMapFile(buf, NX_READONLY);
  331.         
  332.         /* since no file was in our Library, lets put it there */
  333.         NXSaveToFile(aStream, buf);
  334.         NXSeek(aStream, 0 , NX_FROMSTART);
  335.     }
  336.        
  337.     return aStream;
  338. }
  339.  
  340. /*================================================================
  341.  *
  342.  *================================================================*/
  343.  
  344. - showInfoPanel:sender
  345. {
  346.     if (!infoPanel)
  347.     {
  348.         [NXApp loadNibSection:"InfoPanel.nib" owner:self];
  349.     [versionField setStringValue: __DATE__ ];
  350.         [infoPanel setFrameUsingName:info_frame];
  351.     [infoPanel setFrameAutosaveName:info_frame];
  352.     }
  353.     [infoPanel makeKeyAndOrderFront:self];
  354.     return self;
  355. }
  356.  
  357.        
  358. - showPrefPanel:sender
  359. {
  360.     [prefPanel makeKeyAndOrderFront:sender];
  361.     
  362.     return self;
  363. }
  364.  
  365. @end
  366.